package com.mdisoft.quotogram;

import com.mdisoft.quotogram.utils.API;
import com.mdisoft.quotogram.utils.LocationHelper;
import com.mdisoft.quotogram.utils.UserSettings;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;


public class LaunchActivity extends Activity {
	private String TAG = "QUOTOGRAM_Launch";
	
	private API _api;
	private Context _context;
	private UserSettings _s;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_splash);
        
        _context = getApplicationContext();
        _api = new API(_context);
        _s = new UserSettings(_context);
        
        LocationHelper locationHelper = new LocationHelper(_context);
        Location location = locationHelper.LastBestLocation();
        
        AlertDialog dialog;
        AlertDialog.Builder builder = new AlertDialog.Builder(_context);
        
    	if (_api.isOnline()) {
        	if (location!=null) {
        		_s.setLatitude((float)location.getLatitude());
        		_s.setLongitude((float)location.getLongitude());
        		
        		startActivity(new Intent(_context, MainActivity.class));
        		finish();
        	}
        	else {
        		builder.setMessage("To get better experience with Quotogram we recommend to share your location with us. Would you like to enable location service right now?")
    			.setPositiveButton("Yes", new OnClickListener(){
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
					}				
    			})
    			.setNegativeButton("No", new OnClickListener(){
    				public void onClick(DialogInterface dialog, int which) {
    					dialog.dismiss();
    				}
    			});
    			dialog = builder.create();
    			dialog.show();
        	}
        } else {
        	builder.setMessage("Sorry, but there is no internet connection at this moment...")
			.setNeutralButton("Quit", new OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					finish();
				}				
			});
			dialog = builder.create();
			dialog.show();        	
        }
	}	
}
